/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aptech.edu.ixj.impl;

import aptech.edu.ixj.dto.AnswerDto;
import aptech.edu.ixj.dto.QuestionDto;
import aptech.edu.ixj.dto.SurveyDto;
import aptech.edu.ixj.entity.Survey;
import aptech.edu.ixj.interfaces.ISurveyDao;
import aptech.edu.ixj.interfaces.ISurveyLogic;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import nu.xom.Attribute;
import nu.xom.Builder;
import nu.xom.Document;
import nu.xom.Element;
import nu.xom.Node;
import nu.xom.Nodes;
import nu.xom.Serializer;
import nux.xom.xquery.XQueryUtil;

/**
 *
 * @author VAN
 */
public class SurveyLogicImpl implements ISurveyLogic {

    ISurveyDao surveyDao = new SurveyDaoImpl();

    public List<SurveyDto> getListSurvey() {
        List<SurveyDto> surveyDto = new ArrayList<SurveyDto>();
        SurveyDto survey;
        for (Survey sv : surveyDao.getListSurvey()) {
            survey = new SurveyDto();
            survey.setComment(sv.getComment());
            survey.setContent(sv.getContent());
            survey.setId(sv.getId());
            survey.setName(sv.getName());
            survey.setQuestion(new QALogicImpl().getListQuestion(sv.getId()));
            survey.setQuestionId(sv.getQuestionId());
            surveyDto.add(survey);

        }

        return surveyDto;
    }

    public void save() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public boolean removeSurvey(String id) {
        return (surveyDao.removeSurvey(id));

    }

    public void remove(org.w3c.dom.Node node) {
        surveyDao.remove(node);
    }

    public boolean cerate(Survey survey) {
        return (surveyDao.create(survey));
    }

    public List<SurveyDto> readSurvey(String id) {
        List<SurveyDto> surveyDto = new ArrayList<SurveyDto>();
        SurveyDto survey;
        for (Survey sv : surveyDao.readSurvey(id)) {
            survey = new SurveyDto();
            survey.setComment(sv.getComment());
            survey.setContent(sv.getContent());
            survey.setId(sv.getId());
            survey.setQuestion(new QALogicImpl().getListQuestion(sv.getId()));
            survey.setQuestionId(sv.getQuestionId());
            surveyDto.add(survey);
        }
        return surveyDto;
    }

    public static void main(String[] argm) {

        List<SurveyDto> s = new SurveyLogicImpl().readSurvey("2");
        for (SurveyDto surveyDto : s) {

            System.out.println(surveyDto.getContent());
            for(QuestionDto q : surveyDto.getQuestion())
            {
                System.out.println(q.getContent());
                
            }
        }

    }

    public boolean cerate(SurveyDto survey) {
        Survey sv = new Survey();
        sv.setComment(survey.getComment());
        sv.setContent(survey.getContent());
        sv.setName(survey.getName());
        sv.setId(survey.getId());
        sv.setQuestionId(survey.getQuestionId());

        return (surveyDao.create(sv));
    }
}
